﻿#pragma once

#include<iostream>
using namespace std;

enum Colour // 枚举
{
	RED,
	BLACK
};

template<class K,class V>
struct RBtreeNode
{

	pair<K, V> _kv;
	RBtreeNode<K,V>* _left;
	RBtreeNode<K,V>* _right;
	RBtreeNode<K,V>* _parent;
	Colour _col;

	RBtreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}
};

template<class K,class V>
struct RBtree
{
	typedef RBtreeNode<K, V> Node;
public:
	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK; // 新增头结点一定是黑色的
			return true;
		}
		// 先找到插入的位置
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		cur->_col = RED; // 插入节点一定是红色
		if (kv.first > parent->_kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		// 处理连续的红色节点,直到父节点是黑色的
		while (parent && parent->_col == RED)
		{
			// 爷爷节点
			Node* grandfather = parent->_parent;
			
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;

				// 情况1：uncle存在且为红，只需要将g变成红，p和u变成黑
				if (uncle && uncle->_col == RED)
				{
					grandfather->_col = RED;
					uncle->_col = BLACK;
					parent->_col = BLACK;

					// 向上更新
					cur = grandfather; 
					parent = cur->_parent;
				} 
				else 
				{
					// uncle不存在或存在且为黑
					// 情况2/ 情况3  + 变色，具体区分看cur插入在parent的位置
					if (cur == parent->_left) // 情况2(右单旋)
					{
						RotateR(grandfather);
						grandfather->_col = RED;
						parent->_col = BLACK;

						cur = grandfather;
						// cur 需要更新为 grandfather，因为 grandfather 
						// 现在位于插入路径上，我们需要继续检查和调整以确保红黑树的性质不被违反
					}
					else // 情况3 (左右双旋)
					{
						RotateL(parent);
						RotateR(grandfather);

						
						cur->_col = BLACK; // cur成为新的根节点
						grandfather->_col = RED;

						// 不需要更新cur为grandfather，因为cur本身就是
						// 旋转后的关键节点，它现在处于原来grandfather的位置
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;

				// 情况1：uncle存在且为红，只需要将g变成红，p和u变成黑
				if (uncle && uncle->_col == RED)
				{
					grandfather->_col = RED;
					uncle->_col = BLACK;
					parent->_col = BLACK;

					// 向上更新
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_right) //情况2：左单旋
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
						
						cur = grandfather;
					}
					else //情况3：右左双旋
					{
						RotateR(parent);
						RotateL(grandfather);

						cur->_col = BLACK; // cur成为新的根节点
						grandfather->_col = RED;
					}
					break;
				}
				
			}
		}
		_root->_col = BLACK;
		return true;
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* pParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}

			subL->_parent = pParent;
		}
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;
		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}

	/*void RotateR(Node* parent)
		{
			Node* subL = parent->_left;
			Node* subLR = subL->_right;

			if (subLR)
				subLR->_parent = parent;
			parent->_left = subLR;

			Node* grandparent = parent->_parent;
			if (grandparent == nullptr)
			{
				subL->_parent = nullptr;
				_root = subL;
			}
			else
			{
				if (parent == grandparent->_left)
					subL = grandparent->_left;
				else
					subL = grandparent->_right;
				subL->_parent = grandparent;
			}

			subL->_right = parent;
			parent->_parent = subL;
		}

		void RotateL(Node* parent)
		{
			Node* subR = parent->_right;
			Node* subRL = subR->_left;

			if (subRL)
				subRL->_parent = parent;
			parent->_right = subRL;

			Node* grandparent = parent->_parent;
			if (grandparent == nullptr)
			{
				subR->_parent = nullptr;
				_root = subR;
			}
			else
			{
				if (parent == grandparent->_left)
					subR = grandparent->_left;
				else
					subR = grandparent->_right;
				subR->_parent = grandparent;
			}

			subR->_left = parent;
			parent->_parent = subR;
		}*/

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (key > cur->_kv.first)
				cur = cur->_right;
			else if (key < cur->_kv.first)
				cur = cur->_left;
			else
				return cur;
			
		}
		return nullptr;
	}

	bool check(Node* root, int blacknums, const int refnums)
	{
		if (root == nullptr)
		{   // 走到头了,对比该路径黑节点与参考黑节点
			if (refnums != blacknums)
			{
				cout << "存在黑色结点的数量不相等的路径" << endl;
				return false;
			}
			else
				return true;
		}

		// 检查孩子不太方便，因为孩子有两个，且不一定存在，反过来检查父亲就方便多了
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红色结点" << endl;
			return false;
		}

		if (root->_col == BLACK)
			++blacknums;

		return check(root->_left, blacknums, refnums) &&
			check(root->_right, blacknums, refnums);
	}

	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
			return false;

		Node* cur = _root;
		int refnums = 0;
		while (cur) // 以一条路径(最左路径)为参考值，记录黑色节点个数
		{
			if (cur->_col == BLACK)
				++refnums;
			cur = cur->_left;
		}
		return check(_root, 0, refnums);
	}

	

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	int Height()
	{
		return _Height(_root);
	}

	int Size()
	{
		return _Size(_root);
	}

private:
		void _InOrder(Node* root)
		{
			if (root == nullptr)
				return;
			_InOrder(root->_left);
			cout << root->_kv.first << " ";
			_InOrder(root->_right);
		}
		int _Height(Node* root)
		{
			if (root == nullptr)
				return 0;

			int LHight = _Height(root->_left);
			int RHight = _Height(root->_right);
			return LHight > RHight ? LHight + 1 : RHight + 1;
		}
		int _Size(Node* root)
		{
			if (root == nullptr)
				return 0;

			return _Size(root->_left) + _Size(root->_right) + 1;
		}
private:
	Node* _root = nullptr;
	
};
